﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace statue
{
    public static class SqlHelper
    {
        public static readonly string conStrSource =ConfigurationManager.AppSettings["ConnectionStringSource"];
        public static readonly string conStrTarget = ConfigurationManager.AppSettings["ConnectionStringTarget"];
        //ExecuteNonQuery案例
        //string sql = "insert into MyStudent(FName, FAge, FGender, FMath, FEnglish, FClassId, FBirthday) values(@name,@age,@gender,@math,@english,@classid,@birthday)";
        //    SqlParameter[] pms = new SqlParameter[] { 
        //    new SqlParameter("@name",model.FName),
        //    new SqlParameter("@age",model.FAge),
        //    new SqlParameter("@gender",model.FGender),
        //    new SqlParameter("@math",model.FMath==null?DBNull.Value:(object)model.FMath),
        //    new SqlParameter("@english",model.FEnglish),
        //    new SqlParameter("@classid",model.Class.ClassId),
        //    new SqlParameter("@birthday",model.FBirthday)
        //    };
        //    return SqlHelper.ExecuteNonQuery(sql, pms);

        public static int ExecuteNonQuery(string conStr,string sql, params SqlParameter[] pms)
        {
            using (SqlConnection con = new SqlConnection(conStr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    if (pms != null)
                    {
                        cmd.Parameters.AddRange(pms);
                    }
                    con.Open();
                    return cmd.ExecuteNonQuery();
                }
            }
        }

        public static object ExecuteScalar(string conStr, string sql, params SqlParameter[] pms)
        {
            using (SqlConnection con = new SqlConnection(conStr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, con))
                {
                    if (pms != null)
                    {
                        cmd.Parameters.AddRange(pms);
                    }
                    con.Open();
                    return cmd.ExecuteScalar();
                }
            }
        }

        //ExecuteReader案例
         //MyStudent model = null;

         //   string sql = "select * from MyStudent where fid=@id";
         //   using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, new SqlParameter("@id", (object)id)))
         //   {
         //       if (reader.HasRows)
         //       {
         //           while (reader.Read())
         //           {
         //               //Fid, FName, FAge, FGender, FMath, FEnglish, FClassId, FBirthday
         //               model = new MyStudent();
         //               model.Fid = reader.GetInt32(0);
         //               model.FName = reader.GetString(1);
         //               model.FAge = reader.GetInt32(2);
         //               model.FGender = reader.GetString(3);
         //               model.FMath = reader.IsDBNull(4) ? null : (int?)reader.GetInt32(4);
         //               model.FEnglish = reader.GetInt32(5);
         //               model.Class = new MyClass() { ClassId = reader.GetInt32(6) };
         //               model.FBirthday = reader.GetDateTime(7);
         //           }
         //       }
         //   }
         //   return model;

        public static SqlDataReader ExecuteReader(string conStr, string sql, params SqlParameter[] pms)
        {
            SqlConnection con = new SqlConnection(conStr);
            using (SqlCommand cmd = new SqlCommand(sql, con))
            {
                if (pms != null)
                {
                    cmd.Parameters.AddRange(pms);
                }
                try
                {
                    con.Open();
                    return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                }
                catch
                {
                    con.Close();
                    con.Dispose();
                    throw;
                }
            }
        }
        public static DataTable ExecuteDataTable(string conStr, string sql, params SqlParameter[] pms)
        {
            DataTable dt = new DataTable();
            using (SqlDataAdapter adapter = new SqlDataAdapter(sql, conStr))
            {
                adapter.Fill(dt);
            }
            return dt;
        }

    }
}
